package com.youlai.boot.modules.content.controller;

import com.youlai.boot.modules.content.service.LzLinkService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.modules.content.model.form.LzLinkForm;
import com.youlai.boot.modules.content.model.query.LzLinkQuery;
import com.youlai.boot.modules.content.model.vo.LzLinkVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 友链前端控制层
 *
 * @author lz006891
 * @since 2025-02-25 10:49
 */
@Tag(name = "友链接口")
@RestController
@RequestMapping("/api/v1/lzLinks")
@RequiredArgsConstructor
public class LzLinkController  {

    private final LzLinkService lzLinkService;

    @Operation(summary = "友链分页列表")
    @GetMapping("/page")
//    @PreAuthorize("@ss.hasPerm('content:lzLink:query')")
    public PageResult<LzLinkVO> getLzLinkPage(LzLinkQuery queryParams ) {
        IPage<LzLinkVO> result = lzLinkService.getLzLinkPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增友链")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('content:lzLink:add')")
    public Result<Void> saveLzLink(@RequestBody @Valid LzLinkForm formData ) {
        boolean result = lzLinkService.saveLzLink(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取友链表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('content:lzLink:edit')")
    public Result<LzLinkForm> getLzLinkForm(
        @Parameter(description = "友链ID") @PathVariable Long id
    ) {
        LzLinkForm formData = lzLinkService.getLzLinkFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改友链")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('content:lzLink:edit')")
    public Result<Void> updateLzLink(
            @Parameter(description = "友链ID") @PathVariable Long id,
            @RequestBody @Validated LzLinkForm formData
    ) {
        boolean result = lzLinkService.updateLzLink(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除友链")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('content:lzLink:delete')")
    public Result<Void> deleteLzLinks(
        @Parameter(description = "友链ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = lzLinkService.deleteLzLinks(ids);
        return Result.judge(result);
    }
}
